package top.tupobi.zing.utils;

import android.content.Context;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import top.tupobi.zing.InfoRecord;
import top.tupobi.zing.mvp.model.HttpResult;
import top.tupobi.zing.mvp.model.LocationModel;
import top.tupobi.zing.mvp.model.UserExt;
import top.tupobi.zing.network.DefaultObserver;
import top.tupobi.zing.network.HttpConfig;
import top.tupobi.zing.network.beiyong.NetWorkBeiyong;
import top.tupobi.zing.network.beiyong.RxUtils;

public class LocationUtils {

    private LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();

    private LocationUtils() {

    }

    private static class Holder{
        private static LocationUtils instance = new LocationUtils();
    }

    public static LocationUtils getInstance() {
        return Holder.instance;
    }

    public void init(Context context) {
        mLocationClient = new LocationClient(context);
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
    }

    public void startLocationPost() {
        LocationClientOption option = new LocationClientOption();
        //可选，是否需要地址信息，默认为不需要，即参数为false
        //如果开发者需要获得当前点的地址信息，此处必须为true
        option.setIsNeedAddress(true);
        //可选，设置发起定位请求的间隔，int类型，单位ms
        option.setScanSpan(2500);
        //可选，设置是否使用gps，默认false
        //使用高精度和仅用设备两种定位模式的，参数必须设置为true
        option.setOpenGps(true);

        mLocationClient.setLocOption(option);

        //可选，设置返回经纬度坐标类型，默认GCJ02
        //GCJ02：国测局坐标；
        //BD09ll：百度经纬度坐标；
        //BD09：百度墨卡托坐标；
        option.setCoorType("bd09ll");
        mLocationClient.start();
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location){
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取地址相关的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

            String addr = location.getAddrStr();    //获取详细地址信息
            String country = location.getCountry();    //获取国家
            String province = location.getProvince();    //获取省份
            String city = location.getCity();    //获取城市
            String district = location.getDistrict();    //获取区县
            String street = location.getStreet();    //获取街道信息

            double latitude = location.getLatitude();    //获取纬度信息
            double longitude = location.getLongitude();    //获取经度信息
            float radius = location.getRadius();    //获取定位精度，默认值为0.0f

            LocationModel locationModel = new LocationModel();
            locationModel.setAddr(addr);
            locationModel.setCountry(country);
            locationModel.setProvince(province);
            locationModel.setCity(city);
            locationModel.setDistrict(district);
            locationModel.setStreet(street);
            locationModel.setLatitude(latitude);
            locationModel.setLongitude(longitude);
            locationModel.setRadius(radius);

            LogUtils.e("locationModel == " + locationModel);
            if (null == addr || "".equals(addr)) {
                return;
            }

            String locationJson = new Gson().toJson(locationModel);
            UserExt userExt = new UserExt();
            userExt.setOnline(true);
            userExt.setLocationJson(locationJson);


            Map<String, String> params = new HashMap<>();
            params.put("userId", InfoRecord.getUserId());
            params.put("userExt", new Gson().toJson(userExt));

            NetWorkBeiyong.request().getApi()
                    .saveLocation(HttpConfig.SAVE_LOCATION, params)
                    .compose(RxUtils.applySchedulersLife())
                    .subscribe(new DefaultObserver<HttpResult>() {
                        @Override
                        public void onSuccess(HttpResult httpResult) {
                        }
                    });
        }
    }
}
